﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Data;
using Csla.Validation;
using System.Data.SqlClient;

namespace 数据转换小工具
{
    /// <summary>
    /// 配置文件类。
    /// </summary>
    [Serializable()]
    public class Calalog : Csla.BusinessBase<Calalog>
    {
        private Guid _id = Guid.NewGuid();
        private string _datasource = string.Empty;
        private string _name = string.Empty;
        private string _catalog = string.Empty;
        private string _tablename = string.Empty;
        private string _username = string.Empty;
        private string _password = string.Empty;
        private string _version = string.Empty;

        public string DataSource
        {
            get { return _datasource; }
            set { _datasource = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Catalog
        {
            get { return _catalog; }
            set { _catalog = value; }
        }

        public string TableName
        {
            get { return _tablename; }
            set { _tablename = value; }
        }

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public string Version
        {
            get { return _version; }
            set { _version = value; }
        }

        public string Sql
        {
            get
            {
                string sql = string.Empty;
                switch (_version.Trim())
                {
                    case "2000":
                        sql = "Provider=SQLOLEDB.1;Data Source=" + _datasource + ";User ID=" + _username + ";Password=" + _password + ";Database=" + _catalog;
                        break;
                    case "2005":
                        sql = "Provider=SQLOLEDB.1;Data Source=" + _datasource + ";User ID=" + _username + ";Password=" + _password + ";Database=" + _catalog;
                        break;
                    case "2008":
                        sql = "Provider=SQLOLEDB.1;Data Source=" + _datasource + ";User ID=" + _username + ";Password=" + _password + ";Database=" + _catalog;
                        break;
                }
                return sql;
            }
        }


        public Calalog() { }

        public Calalog(string name, string datasource, string calalog, string username, string password, string version)
        {
            this._datasource = datasource;
            this._name = name;
            this._catalog = calalog;
            this._username = username;
            this._password = password;
            this._version = version;
        }
        protected override object GetIdValue()
        {
            return _id;
        }
        internal void DeleteSelf()
        {
            if (!IsDirty) return;
            if (IsNew) return;

            MarkNew();
        }
    }

    [Serializable()]
    public class CalalogDetails : Csla.BusinessListBase<CalalogDetails, Calalog>
    {
        public CalalogDetails()
        {
            MarkAsChild();
        }
        public Calalog NewCalalog()
        {
            return new Calalog();
        }
        public Calalog NewCalalog(string name, string datasource, string calalog, string username, string password, string version)
        {
            return new Calalog(name,datasource,calalog,username,password,version);
        }
    }

    /// <summary>
    /// 数据
    /// </summary>
    public class Date
    {
        private string _clounmname = string.Empty;
        private string _tablename = string.Empty;
        private bool _allownull = true;
        private string _datetype = string.Empty;
        /// <summary>
        /// 字段名
        /// </summary>
        public string ClounmName
        {
            get { return _clounmname; }
            set { _clounmname = value; }
        }
        /// <summary>
        /// 表名。
        /// </summary>
        public string TableName
        {
            get { return _tablename; }
            set { _tablename = value; }
        }
        /// <summary>
        /// 是否可为空。
        /// </summary>
        public bool AllowNull
        {
            get { return _allownull; }
            set { _allownull = value; }
        }
        /// <summary>
        /// 数据类型。
        /// </summary>
        public string DateType
        {
            get { return _datetype; }
            set { _datetype = value; }
        }

        public Date() { }
        public Date(string clounmname, bool allownull, string datetype)
        {
            _clounmname = clounmname;
            _allownull = allownull;
            _datetype = datetype;
        }
    }

    /// <summary>
    /// 表关联。
    /// </summary>
    public class Relevance
    {
        private string _firsttable = string.Empty;
        private string _secondtable = string.Empty;
        private Dictionary<string, string> _relevance = new Dictionary<string, string>();
        /// <summary>
        /// 第一个表名。
        /// </summary>
        public string FirstTable
        {
            get { return _firsttable; }
            set { _firsttable = value; }
        }
        /// <summary>
        /// 第二个表名
        /// </summary>
        public string SecondTable
        {
            get { return _secondtable; }
            set { _secondtable = value; }
        }
        /// <summary>
        /// 关联关系
        /// </summary>
        public Dictionary<string,string> RelevanceList
        {
            get { return _relevance; }
            set { _relevance = value; }
        }
    }
}
